/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.financial.service.incomeexpenditure;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.financial.dao.incomeexpenditure.IncomeExpenditureDao;
import com.jeesite.modules.financial.dao.incomeexpenditure.IncomeExpenditureDetailDao;
import com.jeesite.modules.financial.entity.incomeexpenditure.IncomeExpenditure;
import com.jeesite.modules.financial.entity.incomeexpenditure.IncomeExpenditureDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收支明细单Service
 * @author LV
 * @version 2021-03-04
 */
@Service
@Transactional(readOnly=true)
public class IncomeExpenditureService extends CrudService<IncomeExpenditureDao, IncomeExpenditure> {
	
	@Autowired
	private IncomeExpenditureDetailDao incomeExpenditureDetailDao;
    @Autowired
    private CodeRuleService codeRuleService;
	/**
	 * 获取单条数据
	 * @param incomeExpenditure
	 * @return
	 */
	@Override
	public IncomeExpenditure get(IncomeExpenditure incomeExpenditure) {
		IncomeExpenditure entity = super.get(incomeExpenditure);
		if (entity != null){
			IncomeExpenditureDetail incomeExpenditureDetail = new IncomeExpenditureDetail(entity);
			incomeExpenditureDetail.setStatus(IncomeExpenditureDetail.STATUS_NORMAL);
			entity.setIncomeExpenditureDetailList(incomeExpenditureDetailDao.findList(incomeExpenditureDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param incomeExpenditure 查询条件
	 * @param incomeExpenditure.page 分页对象
	 * @return
	 */
	@Override
	public Page<IncomeExpenditure> findPage(IncomeExpenditure incomeExpenditure) {
		return super.findPage(incomeExpenditure);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param incomeExpenditure
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(IncomeExpenditure incomeExpenditure) {
        if (incomeExpenditure.getIsNewRecord()) {
            incomeExpenditure.setIncomeExpenditureCode(codeRuleService.getCode("incomeExpend"));
        }
		super.save(incomeExpenditure);
        if (incomeExpenditure.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("incomeExpend");
        }
		// 保存上传附件
		FileUploadUtils.saveFileUpload(incomeExpenditure.getId(), "incomeExpenditure_file");
		// 保存 IncomeExpenditure子表
		for (IncomeExpenditureDetail incomeExpenditureDetail : incomeExpenditure.getIncomeExpenditureDetailList()){
			if (!IncomeExpenditureDetail.STATUS_DELETE.equals(incomeExpenditureDetail.getStatus())){
				incomeExpenditureDetail.setIncomeExpenditureId(incomeExpenditure);
				if (incomeExpenditureDetail.getIsNewRecord()){
					incomeExpenditureDetailDao.insert(incomeExpenditureDetail);
				}else{
					incomeExpenditureDetailDao.update(incomeExpenditureDetail);
				}
			}else{
				incomeExpenditureDetailDao.delete(incomeExpenditureDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param incomeExpenditure
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(IncomeExpenditure incomeExpenditure) {
		super.updateStatus(incomeExpenditure);
	}
	
	/**
	 * 删除数据
	 * @param incomeExpenditure
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(IncomeExpenditure incomeExpenditure) {
		super.delete(incomeExpenditure);
		IncomeExpenditureDetail incomeExpenditureDetail = new IncomeExpenditureDetail();
		incomeExpenditureDetail.setIncomeExpenditureId(incomeExpenditure);
		incomeExpenditureDetailDao.deleteByEntity(incomeExpenditureDetail);
	}
	
}